package com.pda.xinma.ui.search;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.xinma.R;
import com.pda.xinma.bean.ScanBean;
import com.pda.xinma.bean.ScanResultBean;
import com.pda.xinma.bean.SearchBean;
import com.pda.xinma.contract.SearchContract;
import com.pda.xinma.presenter.SearchPresenter;
import com.pda.xinma.utils.StringConfig;
import com.pda.xinma.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AppActivityKey.SEARCHACTIVITY)
public class SearchActivity extends BaseMvpActivity<SearchPresenter> implements SearchContract.View {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_container)
    TextView tvContainer;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_picking)
    TextView tvPicking;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;
    @BindView(R.id.tv_detail)
    TextView tvDetail;
    @BindView(R.id.tv_latheName)
    TextView tvLatheName;
    @BindView(R.id.tv_processName)
    TextView tvProcessName;
    @BindView(R.id.tv_articleId)
    TextView tvArticleId;
    @BindView(R.id.tv_model)
    TextView tvModel;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    @BindView(R.id.tv_planNum)
    TextView tvPlanNum;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.ll_content)
    LinearLayout llContent;

    @Override
    public void setScanBean(ScanResultBean scanBean) {
        if (scanBean.getType().equals("KW")) {
            tvStorageBin.setText(scanBean.getCode());
            if (!TextUtils.isEmpty(tvContainer.getText())) {
                llContent.setVisibility(View.GONE);
                presenter.getOrderTaskList(tvContainer.getText().toString(), tvStorageBin.getText().toString());
            }
        } else {
            ToastUtils.showToast(mContext, "请扫接驳工位");

        }
    }

    @Override
    public void setData(SearchBean scanBean) {
        llContent.setVisibility(View.VISIBLE);
        tvArticleId.setText(scanBean.getArticleId());
        tvGrade.setText(scanBean.getGrade());
        tvModel.setText(scanBean.getModel());
        tvLatheName.setText(scanBean.getProOrderNo());
        tvStatus.setText(scanBean.getStatusStr());
        tvProcessName.setText(scanBean.getProcessName());
        tvPlanNum.setText(scanBean.getPlanNmu());
    }

    @Override
    public SearchPresenter getPresenter() {
        return new SearchPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_search;
    }

    @Override
    public void initView() {
        String code = getIntent().getExtras().getString(StringConfig.CODE, "");
        tvStorageBin.setText(code);
        String id = getIntent().getExtras().getString(StringConfig.STORAGEBINID, "");
        tvContainer.setText(id);
    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                if (scanBean.getCode().contains("order")) {
                    int lenth = scanBean.getCode().indexOf("A");
                    String order = scanBean.getCode().substring(lenth + 1);
                    tvContainer.setText(order);
                    if (!TextUtils.isEmpty(tvStorageBin.getText())) {
                        llContent.setVisibility(View.GONE);
                        presenter.getOrderTaskList(order, tvStorageBin.getText().toString());
                    }
                } else {
                    presenter.scanTheQRCodeToVerify(scanBean.getCode());
                }
            }
        });
    }

    @Override
    public void getData() {

    }


    @OnClick(R.id.iv_close)
    public void onViewClicked() {
        finishActivity();
    }


    @OnClick(R.id.tv_detail)
    public void onViewClicked1() {
        if (TextUtils.isEmpty(tvContainer.getText().toString())) {
            showMessage("请先扫生产订单码");
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString("order", tvContainer.getText().toString());
        startIntent(AppActivityKey.ORDERDETAILACTIVITY, bundle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
